package com.rice.riceaiagent.advisor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.chat.client.advisor.api.*;
import org.springframework.ai.chat.model.MessageAggregator;
import reactor.core.publisher.Flux;


/**
 * 自定义日志Advisor
 * @author ricejson
 */
public class MyLoggerAdvisor implements CallAroundAdvisor, StreamAroundAdvisor {

    private static final Logger logger = LoggerFactory.getLogger(MyLoggerAdvisor.class);

    @Override
    public String getName() {
        return this.getClass().getSimpleName();
    }

    @Override
    public int getOrder() {
        return 0;
    }
    @Override
    public AdvisedResponse aroundCall(AdvisedRequest advisedRequest, CallAroundAdvisorChain chain) {
        // 调用前记录请求日志
        logger.info("before req:{}", advisedRequest);
        // 调用执行链
        AdvisedResponse advisedResponse = chain.nextAroundCall(advisedRequest);
        // 调用后记录响应日志
        logger.info("after resp:{}", advisedResponse);
        return advisedResponse;
    }

    @Override
    public Flux<AdvisedResponse> aroundStream(AdvisedRequest advisedRequest, StreamAroundAdvisorChain chain) {
        // 调用前记录请求日志
        logger.info("before req:{}", advisedRequest);
        // 调用执行链
        Flux<AdvisedResponse> flux = chain.nextAroundStream(advisedRequest);
        return new MessageAggregator().aggregateAdvisedResponse(flux, (advisedResponse) -> {
            // 调用后记录响应日志
            logger.info("after resp:{}", advisedResponse);
        });
    }
}
